package com.base.common.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.base.core.context.annotation.AuthenticationCheck;
import com.base.core.context.utils.SpringUtils;
import com.base.core.framework.sql.entity.Base;
import com.base.core.framework.sql.service.impl.SqlBaseServiceImpl;
import com.base.core.mvc.web.BaseWebController;
import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.core.json.JsonObject;
import com.gitee.magic.core.utils.StringUtils;
import com.gitee.magic.framework.head.vo.BaseVO;
import com.gitee.magic.framework.head.vo.ObjectVO;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.query.QueryWrapper;

import io.swagger.v3.oas.annotations.Parameter;

/**
 * 
 * @author start
 *
 */
@SuppressWarnings("unchecked")
@RestController
public class CommonController extends BaseWebController {
	
	@AuthenticationCheck
	@RequestMapping("/v1/common/page/{moduleName}/{entityName}/{page}")
	public ModelAndView page(
			@PathVariable @Parameter(description = "模块名") String moduleName,
			@PathVariable @Parameter(description = "实体名") String entityName,
			@PathVariable @Parameter(description = "页面名") String page) {
		Map<String,Object> params=new HashMap<>(10);
		Map<String,String[]> p=request.getParameterMap();
		for(String k:p.keySet()) {
			Integer i=p.get(k).length;
			if(i==1) {
				String value=p.get(k)[0];
				if(!StringUtils.isEmpty(value)) {
					params.put(k, value);
				}
			}else {
				params.put(k, Arrays.asList(p.get(k)));
			}
		}
		Integer pageIndex=Integer.parseInt(params.getOrDefault("pageIndex", 1).toString());
		Integer pageSize=Integer.parseInt(params.getOrDefault("pageSize", 8).toString());
		params.remove("pageIndex");
		params.remove("pageSize");
		
		QueryWrapper wrapper=new QueryWrapper();
		wrapper.allEq(params);
		wrapper.last("limit "+((pageIndex - 1) * pageSize)+","+pageSize);
		Base entity=(Base)SpringUtils.getBean(entityName);
		ModelAndView view= response(params,getBaseService(entityName).pageWrapper(entity.getClass(), wrapper),
				moduleName+"/"+entityName+"/"+page);
		params.put("pageIndex", pageIndex);
		params.put("pageSize", pageSize);
		addRequestAttribute(view);
		return view;
	}
	
    @AuthenticationCheck
    @GetMapping("/v1/common/edit/{moduleName}/{entityName}/{edit}")
    public ModelAndView edit(
    		@PathVariable @Parameter(description = "模块名") String moduleName,
			@PathVariable @Parameter(description = "实体名") String entityName,
			@PathVariable @Parameter(description = "页面名") String edit,
			@RequestParam @Parameter(description = "Id") Long id) {
    	ModelAndView view=new ModelAndView();
    	view.addObject("vo",getBaseService(entityName).load(id));
		view.setViewName(moduleName+"/"+entityName+"/"+edit);
		addRequestAttribute(view);
    	return view;
    }
	
	@AuthenticationCheck
	@PostMapping("/v1/common/{entityName}")
	public BaseVO save(
			@PathVariable @Parameter(description = "实体名") String entityName,
			@RequestBody JsonObject body) {
		Base entity=(Base)SpringUtils.getBean(entityName);
		getBaseService(entityName).save(ConverterEditorUtils.restoreObject(entity.getClass(), body));
		return response();
	}

	@AuthenticationCheck
	@DeleteMapping("/v1/common/{entityName}")
	public ObjectVO<Integer> remove(
			@PathVariable @Parameter(description = "实体名") String entityName,
			@RequestBody List<Long> ao) {
		return response(getBaseService(entityName).remove(ao));
	}
	
	public SqlBaseServiceImpl<Base, Long> getBaseService(String moduleName){
		return (SqlBaseServiceImpl<Base,Long>)SpringUtils.getBean(moduleName+"Service");
	}
	
}
